home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / stub / exe2coff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  1.6 KB  |  81 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #include <io.h>
  8.  
  9.  
  10. static void
  11. exe2aout(char *fname)
  12. {
  13.   unsigned short header[3];
  14.   int ifile;
  15.   int ofile;
  16.   char buf[4096];
  17.   int rbytes;
  18.   ifile = open(fname, O_RDONLY|O_BINARY);
  19.   if (ifile < 0)
  20.   {
  21.     perror(fname);
  22.     return;
  23.   }
  24.   read(ifile, header, sizeof(header));
  25.   if (header[0] == 0x5a4d)
  26.   {
  27.     long header_offset = (long)header[2]*512L;
  28.     if (header[1])
  29.       header_offset += (long)header[1] - 512L;
  30.     lseek(ifile, header_offset, 0);
  31.     header[0] = 0;
  32.     read(ifile, header, sizeof(header));
  33.     if ((header[0] != 0x010b) && (header[0] != 0x014c))
  34.     {
  35.       fprintf(stderr, "%s does not have an a.out file appended to it\n", fname);
  36.       exit(1);
  37.     }
  38.     lseek(ifile, header_offset, 0);
  39.   }
  40.   else
  41.   {
  42.     fprintf(stderr, "%s is not an .EXE file\n", fname);
  43.     exit(1);
  44.   }
  45.   
  46.   *strrchr(fname, '.') = 0;
  47.   ofile = open(fname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0666);
  48.   if (ofile < 0)
  49.   {
  50.     perror(fname);
  51.     return;
  52.   }
  53.   
  54.   while ((rbytes=read(ifile, buf, 4096)) > 0)
  55.   {
  56.     int wb = write(ofile, buf, rbytes);
  57.     if (wb < 0)
  58.     {
  59.       perror(fname);
  60.       break;
  61.     }
  62.     if (wb < rbytes)
  63.     {
  64.       fprintf(stderr, "%s: disk full\n", fname);
  65.       exit(1);
  66.     }
  67.   }
  68.   close(ifile);
  69.   close(ofile);
  70. }
  71.  
  72. int
  73. main(int argc, char **argv)
  74. {
  75.   int i;
  76.   for (i=1; i<argc; i++)
  77.     exe2aout(argv[i]);
  78.   return 0;
  79. }
  80.  
  81.